// Beginning Game Programming, Second Edition
// Chapter 10
// Paddle_Game program source code file


#include "game.h"

//background image
LPDIRECT3DSURFACE9 back;
LPDIRECT3DDEVICE9 pDevice;

//sprite handler
LPD3DXSPRITE sprite_handler;

//ball sprite
LPDIRECT3DTEXTURE9 gNewGame,gIntroduction,gQuit,gOption,gHighScore,gbanner,p_dan,p_ten_lua;
LPDIRECT3DTEXTURE9 gLNewGame,gLIntroduction,gLQuit,gLOption,gLHighScore;
LPDIRECT3DTEXTURE9 plane_fight[3],truc_thang[9];
LPDIRECT3DTEXTURE9 status_ten_lua,status_menu,status_heart,status_khien,boss_cuoi;






//the wave sound
CSound *sound_bounce;
CSound *sound_hit;
CSound *nhac_nen;
CSound *menu_bip,*s_m16,*s_ten_lua;


//misc
long start = GetTickCount();
HRESULT result;
SPRITE maybay;

//initializes the game
int Game_Init(HWND hwnd)
{
	//set random number seed
	srand(time(NULL));

    //initialize mouse
    if (!Init_Mouse(hwnd))
    {
        MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
        return 0;
    }

    //initialize keyboard
    if (!Init_Keyboard(hwnd))
    {
        MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
        return 0;
    }

    //create sprite handler object
    result = D3DXCreateSprite(d3ddev, &sprite_handler);
    if (result != D3D_OK)
        return 0;
	char s[20];
	int i;
	for (i=0; i<=2; i++)
	{
	sprintf(s,"maybay%d.png",i);
	plane_fight[i] = LoadTexture(s, D3DCOLOR_XRGB(255,0,255));
    if (plane_fight[i] == NULL)
        return 0;
	}

	truc_thang[0] = LoadTexture("resource/tructhang1.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[1] = LoadTexture("resource/tructhang1.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[2] = LoadTexture("resource/tructhang2.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[3] = LoadTexture("resource/tructhang3.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[4] = LoadTexture("resource/tructhang4.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[5] = LoadTexture("resource/tructhang5.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[6] = LoadTexture("resource/tructhang6.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[7] = LoadTexture("resource/tructhang7.png", D3DCOLOR_XRGB(255,0,255));
	truc_thang[8] = LoadTexture("resource/tructhang8.png", D3DCOLOR_XRGB(255,0,255));


    //load the background image
    back = LoadSurface("bg.jpg", NULL);
    if (back == NULL)
        return 0;
	 gbanner = LoadTexture("gbanner.png", D3DCOLOR_XRGB(255,0,255));
    if (gbanner == NULL)
        return 0;
    //load Many menu
    gNewGame = LoadTexture("gNewGame.png", D3DCOLOR_XRGB(255,0,255));
    if (gNewGame == NULL)
        return 0;

	gLNewGame = LoadTexture("gLNewGame.png", D3DCOLOR_XRGB(255,0,255));
    if (gLNewGame == NULL)
        return 0;

	gIntroduction = LoadTexture("gIntroduction.png", D3DCOLOR_XRGB(255,0,255));
    if (gIntroduction == NULL)
        return 0;
	
	gLIntroduction = LoadTexture("gLIntroduction.png", D3DCOLOR_XRGB(255,0,255));
    if (gLIntroduction == NULL)
        return 0;
		
	gHighScore = LoadTexture("gHighScore.png", D3DCOLOR_XRGB(255,0,255));
    if (gHighScore == NULL)
       return 0;
	gLHighScore = LoadTexture("gLHighScore.png", D3DCOLOR_XRGB(255,0,255));
    if (gLHighScore == NULL)
        return 0;

	gOption = LoadTexture("gOption.png", D3DCOLOR_XRGB(255,0,255));
    if (gOption == NULL)
        return 0;
	gLOption = LoadTexture("gLOption.png", D3DCOLOR_XRGB(255,0,255));
    if (gLOption == NULL)
        return 0;

	gQuit = LoadTexture("gQuit.png", D3DCOLOR_XRGB(255,0,255));
    if (gOption == NULL)
        return 0;
	gLQuit = LoadTexture("gLQuit.png", D3DCOLOR_XRGB(255,0,255));
    if (gLQuit == NULL)
        return 0;
	p_dan = LoadTexture("dan.png", D3DCOLOR_XRGB(255,0,255));
    if (p_dan == NULL)
        return 0;
	p_ten_lua = LoadTexture("ten_lua2.png", D3DCOLOR_XRGB(255,0,255));
    if (p_ten_lua == NULL)
        return 0;

	boss_cuoi = LoadTexture("resource/boss.png", D3DCOLOR_XRGB(255,0,255));
    if (boss_cuoi == NULL)
        return 0;


	// texture in status
	status_heart = LoadTexture("resource/status_heart.png", D3DCOLOR_XRGB(255,0,255));
    if (status_heart == NULL)
        return 0;

	status_ten_lua = LoadTexture("resource/tenlua_dung.png", D3DCOLOR_XRGB(255,0,255));
    if (status_ten_lua == NULL)
        return 0;
	status_khien = LoadTexture("resource/status_shield.png", D3DCOLOR_XRGB(255,0,255));
    if (status_khien == NULL)
        return 0;
	status_menu = LoadTexture("resource/status_bar.png", D3DCOLOR_XRGB(255,0,255));
    if (status_menu == NULL)
        return 0;

	nhac_nen = LoadSound("bgmusic.wav");
    if (nhac_nen == NULL)
        return 0;
	   
	menu_bip = LoadSound("blip1.wav");
    if (menu_bip == NULL)
        return 0;
    //load bounce wave file
    sound_bounce = LoadSound("bounce.wav");
    if (sound_bounce == NULL)
        return 0;

    //load the hit wave file
    sound_hit = LoadSound("hit.wav");
    if (sound_hit == NULL)
        return 0;
	s_m16 = LoadSound("m16.wav");
    if (s_m16 == NULL)
        return 0;
	s_ten_lua = LoadSound("rocket.wav");
    if (s_ten_lua == NULL)
        return 0;

	maybay.x = 300;
	maybay.y = 300;
	maybay.width = 121;
	maybay.height = 159;
	maybay.curframe = 1;
    //return okay
    return 1;
}

int Collision(SPRITE sprite1, SPRITE sprite2)
{
    RECT rect1;
    rect1.left = sprite1.x+1;
    rect1.top = sprite1.y+1;
    rect1.right = sprite1.x + sprite1.width-1;
    rect1.bottom = sprite1.y + sprite1.height-1;

    RECT rect2;
    rect2.left = sprite2.x+1;
    rect2.top = sprite2.y+1;
    rect2.right = sprite2.x + sprite2.width-1;
    rect2.bottom = sprite2.y + sprite2.height-1;

    RECT dest;
    return IntersectRect(&dest, &rect1, &rect2);
}

int menuItemSelected = 1;
enum senceMan {MenuGame, NewGame};
enum itemMenu {NewGames, Introduction, Option, Quit};
itemMenu selectItemMenu = NewGames;
senceMan gMan = MenuGame;


D3DXVECTOR3 position(0,0,0);


Sences *game_fight_plane; 

//the main game loop
void Game_Run(HWND hwnd)
{
 //   //ball position vector
	if (game_fight_plane == NULL)
		game_fight_plane = new Sences();




 //   //make sure the Direct3D device is valid
  if (d3ddev == NULL)
       return;

 //   //update mouse and keyboard
 ////   Poll_Mouse();
 Poll_Keyboard();

 //   //after short delay, ready for next frame?
 //   //this keeps the game running at a steady frame rate



   LoopSound(nhac_nen);
   game_fight_plane->chay();


 //   //display the back buffer on the screen
    d3ddev->Present(NULL, NULL, NULL, NULL);

 //   //check for mouse button (to exit program)
   // if (Mouse_Button(0))
     //   PostMessage(hwnd, WM_DESTROY, 0, 0);

 //   //check for escape key (to exit program)
   if (Key_Down(DIK_ESCAPE))
       PostMessage(hwnd, WM_DESTROY, 0, 0);
}

//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{

  

    if (back != NULL)
        back->Release();

    if (sprite_handler != NULL)
        sprite_handler->Release();

    //if (sound_bounce != NULL)
    //    delete sound_bounce;

    //if (sound_hit != NULL)
    //    delete sound_hit;


}

